import { agentRequest, apiRequest } from '@/utils/HttpRequest';

export async function findByAgentSelecttList() {
  //SwaggerResources

  return apiRequest.get<API.SysConfig>('agent/findByBackSelectList');
}

export async function findByAgentBySelectList() {
  return agentRequest.get<API.GlobalStatus[]>('findByBackSelectList');
}

export async function findByAgentBySelecEemutList() {
  return new Promise<any>((resolve, reject) => {
    agentRequest
      .get<API.GlobalStatus[]>('findByBackSelectList')
      .then((res) => {
        //   console.log("category/findBySelectList",res)
        let data = res;
        let dataarr = {};
        for (let i = 0; i < data?.length; i++) {
          let datum = data[i];
          datum['text'] = datum['label'];
          // @ts-ignore
          dataarr[datum['value']] = datum;
        }
        resolve(dataarr);
      })
      .catch((e) => {
        reject(e);
      });
  });
}

export async function findByPageList(params?: any) {
  //SwaggerResources

  return agentRequest.getList<API.Agent>('findByPageList', params);
}

//editAgent
export async function editAgent(params: any) {
  return apiRequest.post<any>('agent/edit', params);
}

//invitationEdit
export async function editInvitation(params: API.AgentInvitationRelationship) {
  return apiRequest.post<API.BaseResult>('agent/invitationEdit', params);
}

export async function findByPageInvitationList(params?: any) {
  //SwaggerResources

  return agentRequest.getList<API.Agent>('findByPageInvitationList', params);
}
//findInvitationCategoryByList
//findByMiniSerachList
export async function findInvitationCategoryByList(params?: any) {
  //SwaggerResources

  return agentRequest.get<API.MenuList[]>(
    'findInvitationCategoryByList',
    params,
  );
}
//findByPageInvitationCategoryIdList
export async function findByPageInvitationCategoryIdList(categoryId: any) {
  //SwaggerResources

  return agentRequest.get<API.AgentInvitationRelationship>(
    'findByPageInvitationCategoryIdList',
    { categoryId: categoryId },
  );
}
export async function agentCategoryState(id: any = 0) {
  return agentRequest.post<any>('agentCategoryState?id=' + id);
}
export async function getByMiniSerachList(type: number = 0) {
  return new Promise<API.AttributeValue[]>((resolve, reject) => {
    findBySelectAgentValueList(type)
      .then((result) => {
        let a = result.data;
        let b = {};
        for (let i = 0; i < a.length; i++) {
          let aElement = a[i];
          b[aElement['value']] = aElement;
        }
        resolve(b);
      })
      .catch((e) => {
        reject(e);
      });
  });
}

export async function findBySelectAgentValueList(type: number = 0) {
  //SwaggerResources
  let attrkey = 'agentkey' + type + 'NUmber';
  return new Promise<API.BaseArrayDataResult<any>>((resolve, reject) => {
    let items = localStorage.getItem(attrkey);
    if (items) {
      var parse = JSON.parse(items);

      resolve(parse);
    } else {
      let url = 'findByMiniSerachList';
      agentRequest
        .getList<any>(url, { type: type })
        .then((res) => {
          localStorage.setItem(attrkey, JSON.stringify(res));
          resolve(res);
        })
        .catch((e) => {
          reject(e);
        });
    }
  });
}

//findByPageBackAgentReChangeList
export async function findByPageBackAgentReChangeList(params?: any) {
  //SwaggerResources

  return agentRequest.getList<any>('findByPageBackAgentReChangeList', params);
}
export function findByMiniAgentList(userId: number) {
  //SwaggerResources

  return agentRequest.get<API.GlobalStastic>('findByMiniList');
}
